<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\LinkModel;

/**
 * 推广链接
 * Class Enter
 * @package Controller
 */
class Link extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加编辑卡券
     * @param array $_data 数据
     * @param int $_link_id 卡券id
     * @return mixed
     */
    public function edit(Array $_data,$_link_id = 0)
    {
        if($_link_id) {
            return LinkModel::where("id",$_link_id)->update($_data);
        } else {
            $_data["uniacid"] = $this->_uniacid;
            $_data["uid"] = $this->_uid;
            $_link_id = LinkModel::createId($_data);
        }
        return $_link_id;
    }

    /**
     * 获取用户卡券列表
     * @param array $_limit
     * @param int $_uid
     * @param string $_type
     * @return LinkModel
     */
    public function getUserAll(Array $_limit,$_uid = 0,$_type = 'qb')
    {
        if(!$_uid) $_uid = $this->_uid;
        $CouponModel = LinkModel::where("uniacid",$this->_uniacid)
                                    ->where("uid",$_uid)
                                    ->where("status","!=",9)
                                    ->limit($_limit[0],$_limit[1])
                                    ->order("id","desc")
                                    ->cache(!KUYUAN_DEBUG);
        return $CouponModel->select();
    }

    /**
     * 获取一个卡券信息
     * @param $_link_id
     * @return LinkModel
     */
    public function get($_link_id)
    {
        return LinkModel::where("id",$_link_id)
                    ->where("status","!=",9)
                    ->cache(!KUYUAN_DEBUG)
                    ->find();
    }

    /**
     * 删除卡券
     * @param $_link_id
     * @return mixed
     */
    public function del($_link_id)
    {
        return LinkModel::where("id",$_link_id)
                            ->delete();
    }
}